<template>
    <div>
        <Spin v-if="modalLoading" size="large" fix></Spin>
        <Row class="button_row">
            <span style="float:left">
                <Button type="primary" @click="add()">上报月度设定</Button>
                <Button type="primary" class="button_item_left" @click="createFullData()" style="margin-left: 10px" :loading="createloading">实时库全量数据生成</Button>
                <Button type="primary" class="button_item_left" @click="checkCurrentData()" style="margin-left: 10px" :loading="checkloading">实时库数据校验</Button>
                <Button type="primary" class="button_item_left" @click="exportExcelNew(1)" style="margin-left: 10px" :loading="ExportLoading">家庭信息导出</Button>
                <Button type="primary" class="button_item_left" @click="exportExcelNew(2)" style="margin-left: 10px" :loading="ExportLoading">人员信息导出</Button>
                <Button type="primary" class="button_item_left" @click="createIncrementData()" style="margin-left: 10px" v-if="false">增量数据生成</Button>
                <Button type="primary" class="button_item_left" @click="currentCopyToBase()" style="margin-left: 10px" v-if="false">实时库转存基础库</Button>
                <Button type="primary" class="button_item_left" @click="baseCopyToCheck()" style="margin-left: 10px" v-if="false">基础库转存校验库</Button>
            </span>
        </Row>
        <Modal v-model="visible" :title="title" width="500" style="position:relative;">
            <Form ref="formData" :model="formData" :label-width="150">
                <FormItem label="数据月度">
                    <DatePicker type="month" v-model="formData.prnt0001" style="width: 150px"
                                format="yyyy-MM"  :editable="false"></DatePicker>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button type="text" size="large" @click="visible = false">取消</Button>
                <Button type="primary" size="large" @click="doSubmit('formData')">确定</Button>
            </div>
        </Modal>
        <div class="table_div">
            <vxe-table border
                       resizable
                       row-id="id"
                       :row-key="true"
                       show-overflow="tooltip"
                       align="center"
                       header-align="center"
                       ref="xTree"
                       :loading="Loading"
                       :radio-config="{highlight: true}"
                       @cell-click="selectChange"
                       :data="tableData">
                <vxe-table-column type="radio" width="60" align="center" fixed="left"/>
                <vxe-table-column field="prnt0001" title="数据月度" align="center" fixed="left"/>
                <vxe-table-column field="prnt0002" title="本月总户数" align="center" fixed="left"/>
                <vxe-table-column field="prnt0003" title="本月总人数" align="center" fixed="left"/>
                <vxe-table-column field="prnt0004" title="保障人数" align="center"/>
                <vxe-table-column field="prnt0005" title="非保障人数" align="center"/>
                <vxe-table-column field="prnt0006" title="低保与非保障互斥人数" align="center"/>
                <vxe-table-column field="prnt0015" title="特困与非保障互斥人数" align="center"/>
                <vxe-table-column field="update_date" width="160" title="处理时间" align="center" :formatter="renderDetailTime"/>
                <!--
                <vxe-table-column field="prnt0007" title="上月总户数" align="center"/>
                <vxe-table-column field="prnt0008" title="未变化户数" align="center"/>
                <vxe-table-column field="prnt0009" title="新增户数" align="center"/>
                <vxe-table-column field="prnt0010" title="注销户数" align="center"/>
                <vxe-table-column field="prnt0011" title="复查户数" align="center"/>-->
            </vxe-table>
            <Page :total="dataCount" :page-size="pageSize" show-total class="paging"
                  @on-change="handleTableChange" :current="pageNo"></Page>
        </div>
        <check-result-modal ref="checkResultModal"/>
    </div>
</template>

<script>
  import { postAction } from '@/api/request'
  import { CommonMixin } from '@/views/common/js/CommonMixin'
  import checkResultModal from './checkResultList.vue'

  export default {
    mixins: [CommonMixin],
    components: {
      checkResultModal
    },
    data () {
      return {
        selectedData: '',
        tableData: [],
        modalLoading: false,
        createloading:false,
        checkloading:false,
        ExportLoading:false,
        Loading: false,
        url: {
          list: '/api/ac/subsistence/ss00019Service/getPageList',
          add: '/api/ac/subsistence/ss00019Service/insertData',
          clearFullData: '/api/ac/subsistence/ss00019Service/deleteFullData',
          createFullData: '/api/ac/subsistence/ss00019Service/createFullData',
          createIncrementData: '/api/ac/subsistence/ss00019Service/createIncrementData',
          currentCopyToBase: '/api/ac/subsistence/ss00019Service/updateCurrentCopyToBase',
          baseCopyToCheck: '/api/ac/subsistence/ss00019Service/updateBaseCopyToCheck',
          checkCurrentData: '/api/ac/subsistence/ss00019Service/updateCheckCurrentData',
          exportFamily: "/api/ac/subsistence/ss00019Service/exportFamily",
          exportPeople: "/api/ac/subsistence/ss00019Service/exportPeople",
        },
        formData: {},
        queryParam:{
          prnt0013:'db'
        },
        visible: false,
        dataCount: 0,
        pageSize: 10,
        modal: false,
        title: '',
        disableSubmit: false
      }
    },
    methods: {
      selectChange ({ row }) {
        this.$refs.xTree.setRadioRow(row)
        this.selectedData = row
      },
      add () {
        let that = this
        that.title = '上报月度设定'
        let record = {}
        that.visible = true
        that.formData = Object.assign({}, record)
      },

      doSubmit () {
        let that = this
        let url = that.url.add
        that.formData.prnt0013 = 'db'
        postAction(url, that.formData).then((res) => {
          if (res.data.errcode === 0) {
            this.$Message.success(res.data.data.msg)
            this.loadData()
            this.visible = false
          } else {
            this.$Message.error(res.data.errmsg)
          }
        }).finally(() => {

        })
      },
      async createFullData () {
        let that = this
        that.createloading = true
        let record = that.selectedData
        if (that.selectedData == '') {
          that.$Message.error('请选择需要生成月度!')
          return false
        }
        let month = record.prnt0001
        if (confirm('确定生成' + month + '全量数据?')) {
          var dataParam = {
            DATAISSUE: month
          }
          that.modalLoading = false
          await that.clearFullData(month);
          postAction(that.url.createFullData, dataParam).then((res) => {
            if (res.data.errcode === 0) {
              that.$Message.success(res.data.data.msg,0)
              that.selectedData = ''
              that.loadData()
              that.createloading = false
            } else {
              that.$Message.error(res.data.errmsg,0)
            }
          }).finally(() => {
            that.createloading = false
          })
        }
      },
      async clearFullData (month) {
        let that = this
        var dataParam = {
          DATAISSUE: month
        }
        await postAction(that.url.clearFullData, dataParam).then((res) => {
        })
      },
      createIncrementData () {
        let that = this
        let record = that.selectedData
        if (that.selectedData == '') {
          that.$Message.error('请选中一条数据!')
          return false
        }
        let month = record.prnt0001
        if (confirm('确定生成' + month + '增量数据?')) {
          var dataParam = {
            DATAISSUE: month
          }
          postAction(that.url.createIncrementData, dataParam).then((res) => {
            if (res.data.errcode === 0) {
              that.$Message.success(res.data.data.msg)
              that.selectedData = ''
              that.loadData()
            } else {
              that.$Message.error(res.data.errmsg)
            }
          }).finally(() => {

          })
        }
      },
      currentCopyToBase () {
        let that = this
        let record = that.selectedData
        if (that.selectedData == '') {
          that.$Message.error('请选中一条数据!')
          return false
        }
        let month = record.prnt0001
        var dataParam = {
          DATAISSUE: month
        }
        postAction(that.url.currentCopyToBase, dataParam).then((res) => {
          if (res.data.errcode === 0) {
            that.$Message.success(res.data.data.msg)
            that.selectedData = ''
            that.loadData()
          } else {
            that.$Message.error(res.data.errmsg)
          }
        }).finally(() => {

        })
      },
      baseCopyToCheck () {
        let that = this
        let record = that.selectedData
        if (that.selectedData == '') {
          that.$Message.error('请选中一条数据!')
          return false
        }
        let month = record.prnt0001
        var dataParam = {
          DATAISSUE: month
        }
        postAction(that.url.baseCopyToCheck, dataParam).then((res) => {
          if (res.data.errcode === 0) {
            that.$Message.success(res.data.data.msg)
            that.selectedData = ''
            that.loadData()
          } else {
            that.$Message.error(res.data.errmsg)
          }
        }).finally(() => {

        })
      },
      checkCurrentData () {
        let that = this
        let record = that.selectedData
        if (that.selectedData == '') {
          that.$Message.error('请选择需要校验月度!')
          return false
        }
        this.checkloading=true;
        let month = record.prnt0001
        var dataParam = {
          DATAISSUE: month,
          bcck0005:'db'
        }
        postAction(that.url.checkCurrentData, dataParam).then((res) => {
          if (res.data.errcode === 0) {
            that.$Message.success(res.data.data.msg)
            that.selectedData = ''
            that.loadData()
            that.Loading = false
            that.$refs.checkResultModal.loadResult(month, 'db', 'current')
          } else {
            that.$Message.error(res.data.errmsg)
          }
        }).finally(() => {
          this.checkloading=false;
        })
      },
      exportExcelNew(type) {
        if (!this.selectedData || !this.selectedData.id) {
          this.$Message.error("请选择需要导出月度！");
          return;
        }
        this.queryParam.month=this.selectedData.prnt0001;
        this.queryParam.type='db';
        if (type == 1) {
          this.url.exportExcel = this.url.exportFamily;
          this.exportFileName = "家庭表D1_FAMILYINFO";
        } else if (type == 2) {
          this.url.exportExcel = this.url.exportPeople;
          this.exportFileName = "人员表D2_PEOPLEINFO";
        } else {
          this.url.exportExcel = "";
          this.exportFileName = "";
        }
        this.exportExcel();
      }
    },
    created () {
      this.loadData()
    },
    watch: {}
  }
</script>
<style scoped>


</style>
